#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;

using System.Web;

using System.Collections;
using WaveMaster.Holidays.Club.Domain;
using WaveMaster.Holidays.Club.Service;

#endregion

namespace WaveMaster.Holidays.Club.Web
{
    public partial class PassengerForm : Form,IPassportParentForm
    {
        Passport m_passport;
        User m_current_user;
        EditMode m_editmode;
        PassportService m_passportService;
        MainForm m_mainForm;
        IPassengerParentForm m_parentForm;
        CheckInService m_bookingService;
        Passenger m_passenger;
        Timer m_timerSearchPassport = new Timer();

        public PassengerForm(string name, Passenger passenger, Passport passport, User currentUser, MainForm mainForm, 
            IPassengerParentForm parentForm, EditMode editmode)
        {
            InitializeComponent();
            m_passport = passport;
            m_current_user = currentUser;
            m_mainForm = mainForm;
            this.Name = name;
            m_parentForm = parentForm;
            m_passenger = passenger;

            initButtonEvent();
            initComboDataSource();
            initService();

            if (passport.m_passportID == 0)
                clearForm();
            else
                loadPassport(); 
            setEditMode(editmode);
            
            enableForm(false);
            
            if (parentForm is ConfirmBookingForm)
            {
                toolBarButtonEdit.Enabled = false;
                toolBarButtonRefresh.Enabled = false;
                toolBarButtonSave.Enabled = false;
                toolBarButtonNewPassport.Enabled = false;
                textBoxPassportNo.ReadOnly = true;
                buttonSearchPassport.Enabled = false;
                buttonAddPassenger.Enabled = toolBarButtonSave.Enabled;
            }
            if (editmode== EditMode.Load)
            {
                toolBarButtonEdit.Enabled = false;
                toolBarButtonRefresh.Enabled = false;
                toolBarButtonSave.Enabled = false;
                toolBarButtonNewPassport.Enabled = false;
                textBoxPassportNo.ReadOnly = true;
                textBoxTicket.ReadOnly = true;
                buttonSearchPassport.Enabled = false;
                toolBarButtonEditPassport.Enabled = false;
                buttonAddPassenger.Enabled = toolBarButtonSave.Enabled;
            }
            if (editmode == EditMode.Edit)
            {
                toolBarButtonEdit.Enabled = false;
                toolBarButtonRefresh.Enabled = false;
                toolBarButtonSave.Enabled = true;
                toolBarButtonNewPassport.Enabled = false;
                textBoxPassportNo.ReadOnly = false;
                textBoxTicket.ReadOnly = false;
                buttonSearchPassport.Enabled = false;
                toolBarButtonEditPassport.Enabled = m_passenger.m_passport.m_passportID > 0;
                buttonAddPassenger.Enabled = toolBarButtonSave.Enabled;
                toolBarButtonSave.Text = "Update";
                buttonAddPassenger.Text = "Update";
            }
            if (editmode == EditMode.Create)
            {
                toolBarButtonEdit.Enabled = false;
                toolBarButtonRefresh.Enabled = false;
                toolBarButtonSave.Enabled = true;
                toolBarButtonNewPassport.Enabled = true;
                textBoxPassportNo.ReadOnly = false;
                textBoxTicket.ReadOnly = false;
                buttonSearchPassport.Enabled = true;
                toolBarButtonEditPassport.Enabled = false;
                buttonAddPassenger.Enabled = toolBarButtonSave.Enabled;
            }
            initMenuButtonEnable();
        }

        private void initMenuButtonEnable()
        {
            menuItemAdd.Enabled = toolBarButtonSave.Enabled;
            menuItemCancel.Enabled = toolBarButtonCancel.Enabled;
            menuItemEdit.Enabled = toolBarButtonEdit.Enabled;
            menuItemRefresh.Enabled = toolBarButtonRefresh.Enabled;
            menuItemRegisterPassport.Enabled = toolBarButtonNewPassport.Enabled;
            menuItemEditPassport.Enabled = toolBarButtonEditPassport.Enabled;
        }

        private void initService()
        {
            m_bookingService = (CheckInService)ServiceFactory.Instance().GetService(ServiceFactory.BOOKING_SERVICE);
            m_passportService = (PassportService)ServiceFactory.Instance().GetService(ServiceFactory.PASSPORT_SERVICE);
            m_timerSearchPassport.Interval = 100;
            m_timerSearchPassport.Tick += new EventHandler(m_timerSearchPassport_Tick);
        }

        void m_timerSearchPassport_Tick(object sender, EventArgs e)
        {
            SearchPassportByCode(textBoxPassportNo.Text);
            m_timerSearchPassport.Stop();
        }

        private void clearForm()
        {
            textBoxPassportNo.Text = "";
            dateTimePickerIssueDate.Value = DateTime.Today;
            dateTimePickerExpireDate.Value = DateTime.Today;
            textBoxCountry.Text = "";
            textBoxOfficeIssue.Text = "";

            textBoxName.Text = "";
            textBoxGender.Text = "";
            textBoxBirthPlace.Text = "";
            dateTimePickerBirthDate.Value = DateTime.Today;
            textBoxNationality.Text = "";
            textBoxPlaceOfResidence.Text = "";
            textBoxOccupation.Text = "";
            textBoxMaritalStatus.Text = "";
            textBoxAddress.Text = "";
            textBoxCity.Text = "";
            textBoxZipCode.Text = "";
            textBoxRemark.Text = "";
            m_passport = new Passport();
            clearErrorProvider();
            enableForm(true);
            setEditMode(EditMode.Create);
            textBoxPassportNo.SelectAll();
        }

        private void initButtonEvent()
        {
            //toolBarButtonClear.Click += new EventHandler(toolBarButtonClear_Click);
            toolBarButtonSave.Click += new EventHandler(toolBarButtonSave_Click);
            //toolBarButtonDelete.Click += new EventHandler(toolBarButtonDelete_Click);
            toolBarButtonRefresh.Click += new EventHandler(toolBarButtonRefresh_Click);
            toolBarButtonEdit.Click += new EventHandler(toolBarButtonEdit_Click);
            //toolBarButtonSearch.Click += new EventHandler(toolBarButtonSearch_Click);
            toolBarButtonCancel.Click += new EventHandler(toolBarButtonCancel_Click);
            toolBarButtonNewPassport.Click += new EventHandler(toolBarButtonNewPassport_Click);
            toolBarButtonEditPassport.Click += new EventHandler(toolBarButtonEditPassport_Click);
        }

        void toolBarButtonEditPassport_Click(object sender, EventArgs e)
        {

            PassportForm frm = new PassportForm(this.Name, m_passenger.m_passport, m_current_user, m_mainForm, this, EditMode.Edit);
            frm.ShowDialog();
        }

        void toolBarButtonNewPassport_Click(object sender, EventArgs e)
        {
            SearchPassportByCode(textBoxPassportNo.Text);

           // PassportForm pfd = new PassportForm(this.Name, new Passport(textBoxPassportNo.Text), m_current_user, m_mainForm, this, EditMode.Create);
           // pfd.ShowDialog();
        }
              
        void toolBarButtonEdit_Click(object sender, EventArgs e)
        {
            setEditMode(EditMode.Edit);
            enableForm(!toolBarButtonEdit.Enabled);
        }

        void toolBarButtonCancel_Click(object sender, EventArgs e)
        {
            if (m_editmode == EditMode.Load)
                this.Close();
            else
            MessageBox.Show("Are you sure to cancel?", "Confirmation", MessageBoxButtons.YesNo,
               MessageBoxIcon.Question, new EventHandler(cancelConfirmation));
        }

        void cancelConfirmation(object sender, EventArgs e)
        {
             MessageBoxWindow msg = (MessageBoxWindow)sender;
             if (msg.DialogResult == DialogResult.Yes)
             {
                 this.Close();
             }
        }
        private void initComboDataSource()
        {
            //IList gender = new ArrayList();
            //gender.Add("Male");
            //gender.Add("Female");
            //comboBoxGender.DataSource = gender;

            //IList married = new ArrayList();
            //married.Add("Married");
            //married.Add("Single");
            //comboBoxMaritalstatus.DataSource = married;
        }

        void toolBarButtonRefresh_Click(object sender, EventArgs e)
        {
            //loadAllUser();
            initComboDataSource();
            if (m_editmode == EditMode.Load || m_editmode == EditMode.Edit)
                loadPassport();
        }

        private void clearErrorProvider()
        {
            errorProviderUser.SetError(textBoxPassportNo, "");
            errorProviderUser.SetError(textBoxTicket, "");
        }
        private bool isValidForm()
        {
            clearErrorProvider();
            bool valid_a = m_passport != null;
            if (!valid_a) errorProviderUser.SetError(textBoxPassportNo, "Passport can not be empty");

            bool valid_b = true;
            if (textBoxTicket.Text.Trim() != "")
            {
                if (m_editmode != EditMode.Edit)
                    valid_b = m_bookingService.IsValidTicket(textBoxTicket.Text.Trim());
                else
                {
                    if (m_passenger.m_ticketNo.Trim() != textBoxTicket.Text.Trim())
                        valid_b = m_bookingService.IsValidTicket(textBoxTicket.Text.Trim());
                }
            }
            if (!valid_b) errorProviderUser.SetError(textBoxTicket, "Ticket already been used");
            return valid_a && valid_b;
        }

        void toolBarButtonSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (isValidForm())
                {
                   // m_passport.m_passportNo = textBoxPassportNo.Text;
                   // m_passport.m_issueDate = dateTimePickerIssueDate.Value;
                   // m_passport.m_expireDate = dateTimePickerExpireDate.Value;
                   // m_passport.m_country = textBoxCountry.Text;
                   // m_passport.m_officeIssue = textBoxOfficeIssue.Text;

                   // m_passport.m_name = textBoxName.Text;
                   //// m_passport.m_gender = comboBoxGender.SelectedItem.ToString();
                   // m_passport.m_birthPlace = textBoxBirthPlace.Text;
                   // m_passport.m_birthDate = dateTimePickerBirthDate.Value;
                   // m_passport.m_nationality = textBoxNationality.Text;
                   // m_passport.m_placeOfResidence = textBoxPlaceOfResidence.Text;
                   // m_passport.m_occupation = textBoxOccupation.Text;
                   //// m_passport.m_maritalStatus = comboBoxMaritalstatus.SelectedItem.ToString();
                   // m_passport.m_address = textBoxAddress.Text;
                   // m_passport.m_city = textBoxCity.Text;
                   // m_passport.m_zipcode = textBoxZipCode.Text;
                   // m_passport.m_remark = textBoxRemark.Text;
                   // if (m_passport.m_passportID > 0)
                   // {
                   //     m_passport.m_modfctnBy = m_current_user.UserID;
                   //     m_passportService.UpdatePassport(m_passport);
                   // }
                   // else
                   // {
                   //     m_passport.m_cretnBy = m_current_user.UserID;
                   //     m_passportService.InserPassport(m_passport);
                   // }
                   
                    m_passenger.m_passport = m_passport;
                    m_passenger.m_ticketNo = textBoxTicket.Text;
                    if (m_editmode == EditMode.Edit)
                    {
                        m_parentForm.UpdatePassenger(m_passenger);
                        this.Close();
                    }
                    else
                    {
                        if (m_parentForm.isAlreadyExist(m_passenger))
                        {
                            //MessageBox.Show("Passport : '"+m_passenger.m_passport.m_passportNo+"' already checked in in this trip", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                            MessageBox.Show(null, "Passport : '" + m_passenger.m_passport.m_passportNo + "' already checked in in this trip", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, new EventHandler(closeMessageBoxForm));
                            // textBoxPassportNo.Focus();
                            //textBoxPassportNo.SelectAll();
                        }
                        else
                        {
                            m_parentForm.AddPassenger(m_passenger);
                            this.Close();
                        }
                    }
                    //  MessageBox.Show("Record has been saved.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                    //MessageBox.Show("Record has been saved", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, new EventHandler(closeForm));
                    //clearForm();
                    //loadAllUser();
                    //setEditMode(EditMode.Saved);
                    // this.Close();
                }
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }

        }

       

        void closeMessageBoxForm(object sender, EventArgs e)
        {
            textBoxPassportNo.Focus();
            textBoxPassportNo.SelectAll();
        }

        void closeForm(object sender, EventArgs e)
        {
            this.Close();
        }

        private void setEditMode(EditMode editMode)
        {
            toolBarButtonSave.Enabled = (editMode == EditMode.Create || editMode == EditMode.Edit || editMode == EditMode.Saved || editMode == EditMode.Delete)&& m_current_user.RoleDetails(this.Name).NewFlag;
            toolBarButtonEdit.Enabled = (editMode == EditMode.Load) && m_current_user.RoleDetails(this.Name).UpdateFlag;
            buttonAddPassenger.Enabled = toolBarButtonSave.Enabled;
            m_editmode = editMode;
            buttonSearchPassport.Enabled = toolBarButtonSave.Enabled;
            labelEditMode.Text = "[" + editMode + "]";
        }

        private void enableForm(bool enable)
        {
            textBoxPassportNo.ReadOnly = !enable;
            textBoxTicket.ReadOnly = !enable;
            dateTimePickerIssueDate.Enabled = enable;
            dateTimePickerExpireDate.Enabled = enable;
            textBoxCountry.ReadOnly = !enable;
            textBoxOfficeIssue.ReadOnly = !enable;
            textBoxName.ReadOnly = !enable;
            textBoxGender.ReadOnly = !enable;
            textBoxBirthPlace.ReadOnly = !enable;
            dateTimePickerBirthDate.Enabled = enable;
            textBoxNationality.ReadOnly = !enable;
            textBoxIDnumber.ReadOnly = !enable;
            textBoxAddress.ReadOnly = !enable;
            textBoxCity.ReadOnly = !enable;
            textBoxPlaceOfResidence.ReadOnly = !enable;
            textBoxZipCode.ReadOnly = !enable;
            textBoxOccupation.ReadOnly = !enable;
            textBoxMaritalStatus.ReadOnly = !enable;
            textBoxRemark.ReadOnly = !enable;
        }

        private void loadPassport()
        {
            textBoxTicket.Text = m_passenger.m_ticketNo;
            textBoxPassportNo.Text = m_passport.m_passportNo;
            dateTimePickerIssueDate.Value = m_passport.m_issueDate;
            dateTimePickerExpireDate.Value = m_passport.m_expireDate;
            textBoxCountry.Text = m_passport.m_country;
            textBoxOfficeIssue.Text = m_passport.m_officeIssue;

            textBoxName.Text = m_passport.m_name;
            textBoxGender.Text = m_passport.m_gender.ToString();
            textBoxBirthPlace.Text = m_passport.m_birthPlace;
            dateTimePickerBirthDate.Value = m_passport.m_birthDate;
            textBoxNationality.Text = m_passport.m_nationality;
            textBoxPlaceOfResidence.Text = m_passport.m_placeOfResidence;
            textBoxOccupation.Text = m_passport.m_occupation;
            textBoxMaritalStatus.Text = m_passport.m_maritalStatus.ToString();
            textBoxAddress.Text = m_passport.m_address;
            textBoxCity.Text = m_passport.m_city;
            textBoxZipCode.Text = m_passport.m_zipcode;
            textBoxRemark.Text = m_passport.m_remark;
            textBoxIDnumber.Text = m_passport.m_idnumber;

            if (m_passenger.m_passangerID > 0)
            {
                dateTimePickerIssueDate.BackColor = m_passport.m_issueDate == m_passenger.m_issueDate ? Color.Cornsilk : Color.Red;
                dateTimePickerExpireDate.BackColor = m_passport.m_expireDate == m_passenger.m_expireDate ? Color.Cornsilk : Color.Red;
                textBoxCountry.ForeColor = m_passport.m_country == m_passenger.m_country ? Color.Black : Color.Red;
                textBoxOfficeIssue.ForeColor = m_passport.m_officeIssue == m_passenger.m_officeIssue ? Color.Black : Color.Red;
                textBoxPassportNo.ForeColor = m_passport.m_passportNo == m_passenger.m_passportNo ? Color.Black : Color.Red;
                textBoxName.ForeColor = m_passport.m_name == m_passenger.m_name ? Color.Black : Color.Red;
                textBoxGender.ForeColor = m_passport.m_gender.ToString() == m_passenger.m_gender.ToString() ? Color.Black : Color.Red;
                textBoxBirthPlace.ForeColor = m_passport.m_birthPlace == m_passenger.m_birthPlace ? Color.Black : Color.Red;
                dateTimePickerBirthDate.BackColor = m_passport.m_birthDate == m_passenger.m_birthDate ? Color.Cornsilk : Color.Red;
                textBoxNationality.ForeColor = m_passport.m_nationality == m_passenger.m_nationality ? Color.Black : Color.Red;
                textBoxPlaceOfResidence.ForeColor = m_passport.m_placeOfResidence == m_passenger.m_placeOfResidence ? Color.Black : Color.Red;
                textBoxOccupation.ForeColor = m_passport.m_occupation == m_passenger.m_occupation ? Color.Black : Color.Red;
                textBoxMaritalStatus.ForeColor = m_passport.m_maritalStatus.ToString() == m_passenger.m_maritalStatus.ToString() ? Color.Black : Color.Red;
                textBoxAddress.ForeColor = m_passport.m_address == m_passenger.m_address ? Color.Black : Color.Red;
                textBoxCity.ForeColor = m_passport.m_city == m_passenger.m_city ? Color.Black : Color.Red;
                textBoxZipCode.ForeColor = m_passport.m_zipcode == m_passenger.m_zipcode ? Color.Black : Color.Red;
                textBoxRemark.ForeColor = m_passport.m_remark == m_passenger.m_remark ? Color.Black : Color.Red;
                textBoxIDnumber.ForeColor = m_passport.m_idnumber == m_passenger.m_idnumber ? Color.Black : Color.Red;
            }

            if (m_passenger.m_passengerStatus == PassengerStatus.ReCheckIn)
            {
                m_passenger.PassangerHistory = m_bookingService.GetPassengerHistoryByPassengerID(m_passenger.m_passangerID);
                if (m_passenger.PassangerHistory != null)
                {
                    dateTimePickerIssueDate.BackColor = m_passport.m_issueDate == m_passenger.PassangerHistory.m_issueDate ? Color.Cornsilk : Color.Red;
                    dateTimePickerExpireDate.BackColor = m_passport.m_expireDate == m_passenger.PassangerHistory.m_expireDate ? Color.Cornsilk : Color.Red;
                    textBoxCountry.ForeColor = m_passport.m_country == m_passenger.PassangerHistory.m_country ? Color.Black : Color.Red;
                    textBoxOfficeIssue.ForeColor = m_passport.m_officeIssue == m_passenger.PassangerHistory.m_officeIssue ? Color.Black : Color.Red;
                    textBoxName.ForeColor = m_passport.m_name == m_passenger.PassangerHistory.m_name ? Color.Black : Color.Red;
                    textBoxGender.ForeColor = m_passport.m_gender.ToString() == m_passenger.PassangerHistory.m_gender.ToString() ? Color.Black : Color.Red;
                    textBoxBirthPlace.ForeColor = m_passport.m_birthPlace == m_passenger.PassangerHistory.m_birthPlace ? Color.Black : Color.Red;
                    dateTimePickerBirthDate.BackColor = m_passport.m_birthDate == m_passenger.PassangerHistory.m_birthDate ? Color.Cornsilk : Color.Red;
                    textBoxNationality.ForeColor = m_passport.m_nationality == m_passenger.PassangerHistory.m_nationality ? Color.Black : Color.Red;
                    textBoxPlaceOfResidence.ForeColor = m_passport.m_placeOfResidence == m_passenger.PassangerHistory.m_placeOfResidence ? Color.Black : Color.Red;
                    textBoxOccupation.ForeColor = m_passport.m_occupation == m_passenger.PassangerHistory.m_occupation ? Color.Black : Color.Red;
                    textBoxMaritalStatus.ForeColor = m_passport.m_maritalStatus.ToString() == m_passenger.PassangerHistory.m_maritalStatus.ToString() ? Color.Black : Color.Red;
                    textBoxAddress.ForeColor = m_passport.m_address == m_passenger.PassangerHistory.m_address ? Color.Black : Color.Red;
                    textBoxCity.ForeColor = m_passport.m_city == m_passenger.PassangerHistory.m_city ? Color.Black : Color.Red;
                    textBoxZipCode.ForeColor = m_passport.m_zipcode == m_passenger.PassangerHistory.m_zipcode ? Color.Black : Color.Red;
                    textBoxRemark.ForeColor = m_passport.m_remark == m_passenger.PassangerHistory.m_remark ? Color.Black : Color.Red;
                    textBoxTicket.ForeColor = m_passenger.m_ticketNo == m_passenger.PassangerHistory.m_ticketNo ? Color.Black : Color.Red;
                    textBoxIDnumber.ForeColor = m_passport.m_idnumber == m_passenger.PassangerHistory.m_idnumber ? Color.Black : Color.Red;

                }
            }
            
        }

        private void textBoxPassportNo_TextChangedX(object sender, EventArgs e)
        {
         //if (m_passportKey == Keys.Enter)
         //   {
         //       SearchPassportByCode(textBoxPassportNo.Text.Trim());
         //   }
         //   passsportNo = textBoxPassportNo.Text.Trim();
        }
        
       // Keys m_passportKey;

        private void textBoxPassportNo_KeyDownX(object objSender, KeyEventArgs objArgs)
        {
            //m_passportKey = objArgs.KeyData;
            //if (m_passportKey == Keys.Enter)
            //{
            //    if (textBoxPassportNo.Text.Trim() == passsportNo)
            //        SearchPassportByCode(passsportNo);
            //}
        }

        private void textBoxPassportNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == Convert.ToChar(Keys.Enter))
            {
                m_timerSearchPassport.Start();
            }
        }

        private void SearchPassportByCode(string code)
        {
            Passport p = m_passportService.GetPassportByPassportNo(code);
            if (p != null)
            {
                m_passport = p;
                loadPassport();
                textBoxTicket.Focus();
                textBoxTicket.SelectAll();
            }
            else
            {
                PassportForm pfd = new PassportForm(this.Name, new Passport(code), m_current_user, m_mainForm, this, EditMode.Create);
                pfd.ShowDialog();
            }
        }

        public void LoadAllPassport()
        {
           
        }


        public void LoadPassport(Passport p)
        {
            m_passport = p;
            loadPassport();
            textBoxTicket.Focus();
            textBoxTicket.SelectAll();
        }

        private void buttonSearchPassport_Click(object sender, EventArgs e)
        {
            SearchPassportForm sch = new SearchPassportForm(this, textBoxPassportNo.Text.Trim());
            sch.ShowDialog();
        }

        private void PassengerForm_Load(object sender, EventArgs e)
        {
            textBoxPassportNo.Focus();
            textBoxPassportNo.SelectAll();
        }

        private void textBoxTicket_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == Convert.ToChar(Keys.Enter))
            {
                buttonAddPassenger.Focus();
                buttonAddPassenger.Select();
            }
        }
        public void SetActiveCursor()
        {
            textBoxTicket.Focus();
            textBoxTicket.SelectAll();
        }
    }
}